package site.eblog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import site.eblog.service.TagService;

@Controller
public class TagController {
	
	@Autowired TagService tagService;
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping("getTags")
	@ResponseBody
	public List<String> getTags(){
		List<String> tags = tagService.getTags();
		return tags;
	}
	
	@RequestMapping("addTag")
	@ResponseBody
	public int addTag(HttpServletRequest request){
		String name = request.getParameter("name");
		int res = tagService.addTag(name);
		return res;
	}
	
	@RequestMapping("updateTag")
	@ResponseBody
	public int updateTag(HttpServletRequest request){
		String newTagName = request.getParameter("newTagName");
		String oldTagName = request.getParameter("oldTagName");
		Map<String,String> map = new HashMap<>();
		map.put("newTagName", newTagName);
		map.put("oldTagName", oldTagName);
		int res = tagService.updateTag(map);
		return res;
	}
	
	@RequestMapping("delTag")
	@ResponseBody
	public int delTag(HttpServletRequest request){
		String tagName = request.getParameter("tagName");
		int res = tagService.delTag(tagName);
		return res;
	}
}
